//
//  HomeViewModel.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/2/3.
//

import Foundation
import SwiftUI
class HomeViewModel : BaseViewModel {
    @Published var tableItem : [HomeTableItem] = []
    var commentDiaryBean: DiaryBean? = nil //评论的日记
    var commentAiRoleId: String? = nil // 评论的角色
    @Published var showComment = false

    override init() {
        super.init()
        self.addObserve(self, .aiDiary) { (selfInstance, id: String) in
            showLog("执行更新红点")
            //ai日记更新
            if let index = selfInstance.tableItem.firstIndex(where: { $0.id == id}) {
                selfInstance.tableItem[index].red = true //更新红点
            }
        }
        self.addObserve(self, .selectAiRole) { (selfInstance, update: Bool) in
            //刷新ai列表
            selfInstance.getTab(false)
        }
        self.addObserve(self, .updateAiRole) { (selfInstance, update: Bool) in
            //刷新ai列表
            selfInstance.getTab(true)
        }
        self.addObserve(self, .push) { (selfInstance, update: Bool) in
            //推送通知，则检测任务
            TaskExt.checkTask()
        }
        self.addObserve(self, .background) { (selfInstance, status: Bool) in
            //前后台通知，
            if !status {
                //返回前台则检测任务
                TaskExt.checkTask()
            }
        }
        self.addObserve(self, .showComment) { (selfInstance, item: (DiaryBean,String)) in
            //展开评论
            selfInstance.commentDiaryBean = item.0
            selfInstance.commentAiRoleId = item.1
            selfInstance.showComment = true
        }
    }
    
    func getTab(_ force: Bool = false) {
        BackupsManager.shared.setAccount() //设置账号
        request(isLoading: true) {
            if AppStorageManager.shared.toggleAiDiary {
                if force {
                    BackupsManager.shared.getDelegateConfig()?.clearRoleList()
                }
                let list = try await BackupsManager.shared.getDelegateConfig()?.getSelectAiRoleList().prefix(5)
                return list
            } else {
                return []
            }
        } suc: { list in
            var listItems = list?.map {
                return HomeTableItem(uuid: UUID().uuidString, id: $0.objectId ?? "", name: $0.name ?? "")
            } ?? []
            let userName = AppStorageManager.shared.toggleAiDiary ? userManager.getName() : "" //关闭ai日记，则隐藏名字
            let bean = HomeTableItem(uuid: UUID().uuidString,id: userManager.getUserId(), name:  userName)
            listItems.insert(bean,at: 0)
            self.tableItem = listItems
            //创建任务
            TaskExt.createTask()
            //检测任务
            TaskExt.checkTask()
        } fail: {
            showLog($0.message)
        }
    }
    
    func setBackground(aiRoleId: String) {
        request(isLoading: false) {
            let aiRoleBean : AiRoleBean = try await BackupsManager.shared.getDelegateConfig()?.getAiRole(roleId: aiRoleId) ?? AiRoleBean()
            return aiRoleBean
        } suc: { aiRoleBean in
            let url = aiRoleBean.diaryBg
            backgroundManager.setBackground(url)
        } fail: { _ in
        }
    }
}
